package com.net.framework.help.statusbar;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.net.framework.help.rom.RomUtil;
import com.net.framework.help.rom.Target;
import com.net.framework.help.utils.LogUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Author：xiaobin.zhang
 * Email：154722566@qq.com
 * Date：2016/12/17 16:02
 * Desc：<--功能描述-->
 */

public class StatusBarTextColorUtil {

    /**
     * 设置状态栏黑色字体图标，
     * 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android
     *
     * @param activity
     * @return 1:MIUUI 2:Flyme 3:android6.0
     */
    public static void StatusBarLightMode(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (RomUtil.rom() == Target.MIUI) {//小米
                if (getSystemProperty()) {
                    MSystemLightMode(activity, true);
                } else {
                    MIUISetStatusBarLightMode(activity.getWindow(), true);
                }
            } else if (RomUtil.rom() == Target.FLYME) {//魅族数据
                StatusbarColorUtils.setStatusBarDarkIcon(activity.getWindow(), true);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//其它6.0以上的手机
                MSystemLightMode(activity, true);
            }
        }
    }

    /**
     * 清除MIUI或flyme或6.0以上版本状态栏黑色字体
     */
    public static void StatusBarDarkMode(Activity activity) {
        if (RomUtil.rom() == Target.MIUI) {//小米手机
            if (getSystemProperty()) {
                MSystemLightMode(activity, false);
            } else {
                MIUISetStatusBarLightMode(activity.getWindow(), false);
            }
        } else if (RomUtil.rom() == Target.FLYME) {//魅族手机
            StatusbarColorUtils.setStatusBarDarkIcon(activity.getWindow(), false);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//
            MSystemLightMode(activity, false);
        }
    }

    /**
     * 设置状态栏字体图标为深色和白色，需要MIUIV6以上
     *
     * @param window 需要设置的窗口
     * @param dark   是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    public static boolean MIUISetStatusBarLightMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if (dark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }


    /**
     * 设置6.0以上系统状态栏高亮，和黑色
     *
     * @param activity
     * @param bDark    是否把状态栏字体及图标颜色设置为高亮
     */
    private static void MSystemLightMode(Activity activity, boolean bDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = activity.getWindow();
            View decorView = window.getDecorView();
            if (decorView != null) {
                int vis = decorView.getSystemUiVisibility();
                if (bDark) {
                    //已下这个判断，十分重要；不加该判断，在底部有虚拟键盘的华为6.0以上的系统手机上运行，UI底部会有一部分显示不全
                    if (RomUtil.rom() == Target.MIUI) {
                        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    }
//                      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else {
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }

    /**
     * 获取miui手机系统版本号
     *
     * @return
     */
    public static boolean getSystemProperty() {
        String line;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + "ro.miui.ui.version.name");
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
            LogUtils.println("miui系统版本号====" + line);
        } catch (IOException ex) {
            LogUtils.println("无法读取sysprop");
            return false;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    LogUtils.println("Exception while closing InputStream==" + e);
                }
            }
        }
        return line.equals("V9");
    }
}
